import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:panda/base/Constant.dart';
import 'package:panda/base/SignType.dart';
import 'package:panda/http/Api.dart';
import 'package:panda/http/HttpUtil.dart';
import 'package:panda/util/AppSaveInfoUtil.dart';
import 'package:panda/util/LoadingUtil.dart';
import 'package:panda/util/SignUtil.dart';

class ApplyShouQuanPage extends StatefulWidget {
  ApplyShouQuanPage({Key key}) : super(key: key);

  @override
  _ApplyShouQuanPageState createState() => _ApplyShouQuanPageState();
}

class _ApplyShouQuanPageState extends State<ApplyShouQuanPage> {
  String token = "",
      name = "",
      company = "",
      wx_name = "",
      wx_account = "",
      leven = "";
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getString(TOKEN).then((value) {
      token = value;
    });
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(
        title: Text(
          '授权证书申请',
          style: TextStyle(
              color: black,
              fontSize: ScreenUtil.getInstance().setSp(BIG_FONT_SIZE)),
        ),
        elevation: 0.2,
        backgroundColor: white,
        iconTheme: new IconThemeData(color: black_99),
        brightness: Brightness.light,
      ),
      body: Container(
          width: double.infinity,
          color: white,
          child: new Column(
            children: <Widget>[
              Container(
                  height: ScreenUtil.getInstance().setHeight(DP_30),
                  color: f0Color),
              Container(
                padding: EdgeInsets.only(
                    top: ScreenUtil.getInstance().setHeight(DP_10),
                    left: ScreenUtil.getInstance().setHeight(DP_20),
                    right: ScreenUtil.getInstance().setHeight(DP_20)),
                child: Row(children: <Widget>[
                  Container(
                    width: ScreenUtil.getInstance().setWidth(DP_150),
                    child: Text(
                      '公司名称',
                      style: TextStyle(
                          fontSize:
                              ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
                    ),
                  ),
                  new Expanded(
                      child: new TextField(
                    keyboardAppearance: Brightness.light,
                    keyboardType: TextInputType.numberWithOptions(),
                    maxLines: 1,
                    onChanged: (value) {
                      company = value;
                    },
                    style: TextStyle(
                        fontSize:
                            ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
                    cursorColor: mainColor,
                    decoration: new InputDecoration(
                        hintText: "请输入公司名称",
                        border: InputBorder.none,
                        hintStyle: TextStyle(
                            color: grey,
                            fontSize: ScreenUtil.getInstance()
                                .setSp(NORMAL_FONT_SIZE))),
                  )),
                ]),
              ),
              Container(
                height: 1,
                color: f0Color,
              ),
              Container(
                padding: EdgeInsets.only(
                    top: ScreenUtil.getInstance().setHeight(DP_10),
                    left: ScreenUtil.getInstance().setHeight(DP_20),
                    right: ScreenUtil.getInstance().setHeight(DP_20)),
                child: Row(children: <Widget>[
                  Container(
                    width: ScreenUtil.getInstance().setWidth(DP_150),
                    child: Text(
                      '姓名',
                      style: TextStyle(
                          fontSize:
                              ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
                    ),
                  ),
                  new Expanded(
                      child: new TextField(
                    keyboardAppearance: Brightness.light,
                    keyboardType: TextInputType.numberWithOptions(),
                    maxLines: 1,
                    onChanged: (value) {
                      name = value;
                    },
                    style: TextStyle(
                        fontSize:
                            ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
                    cursorColor: mainColor,
                    decoration: new InputDecoration(
                        hintText: "请输入姓名",
                        border: InputBorder.none,
                        hintStyle: TextStyle(
                            color: grey,
                            fontSize: ScreenUtil.getInstance()
                                .setSp(NORMAL_FONT_SIZE))),
                  )),
                ]),
              ),
              Container(
                height: 1,
                color: f0Color,
              ),
              Container(
                padding: EdgeInsets.only(
                    top: ScreenUtil.getInstance().setHeight(DP_10),
                    left: ScreenUtil.getInstance().setHeight(DP_20),
                    right: ScreenUtil.getInstance().setHeight(DP_20)),
                child: Row(children: <Widget>[
                  Container(
                    width: ScreenUtil.getInstance().setWidth(DP_150),
                    child: Text(
                      '微信名',
                      style: TextStyle(
                          fontSize:
                              ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
                    ),
                  ),
                  new Expanded(
                      child: new TextField(
                    keyboardAppearance: Brightness.light,
                    keyboardType: TextInputType.numberWithOptions(),
                    maxLines: 1,
                    onChanged: (value) {
                      wx_name = value;
                    },
                    style: TextStyle(
                        fontSize:
                            ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
                    cursorColor: mainColor,
                    decoration: new InputDecoration(
                        hintText: "请输入微信名称",
                        border: InputBorder.none,
                        hintStyle: TextStyle(
                            color: grey,
                            fontSize: ScreenUtil.getInstance()
                                .setSp(NORMAL_FONT_SIZE))),
                  )),
                ]),
              ),
              Container(
                height: 1,
                color: f0Color,
              ),
              Container(
                padding: EdgeInsets.only(
                    top: ScreenUtil.getInstance().setHeight(DP_10),
                    left: ScreenUtil.getInstance().setHeight(DP_20),
                    right: ScreenUtil.getInstance().setHeight(DP_20)),
                child: Row(children: <Widget>[
                  Container(
                    width: ScreenUtil.getInstance().setWidth(DP_150),
                    child: Text(
                      '微信号',
                      style: TextStyle(
                          fontSize:
                              ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
                    ),
                  ),
                  new Expanded(
                      child: new TextField(
                    keyboardAppearance: Brightness.light,
                    keyboardType: TextInputType.numberWithOptions(),
                    maxLines: 1,
                    onChanged: (value) {
                      wx_account = value;
                    },
                    style: TextStyle(
                        fontSize:
                            ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
                    cursorColor: mainColor,
                    decoration: new InputDecoration(
                        hintText: "请输入微信号",
                        border: InputBorder.none,
                        hintStyle: TextStyle(
                            color: grey,
                            fontSize: ScreenUtil.getInstance()
                                .setSp(NORMAL_FONT_SIZE))),
                  )),
                ]),
              ),
              Container(
                height: 1,
                color: f0Color,
              ),
              Container(
                padding: EdgeInsets.only(
                    top: ScreenUtil.getInstance().setHeight(DP_10),
                    left: ScreenUtil.getInstance().setHeight(DP_20),
                    right: ScreenUtil.getInstance().setHeight(DP_20)),
                child: Row(children: <Widget>[
                  Container(
                    width: ScreenUtil.getInstance().setWidth(DP_150),
                    child: Text(
                      '等级',
                      style: TextStyle(
                          fontSize:
                              ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
                    ),
                  ),
                  new Expanded(
                      child: new TextField(
                    keyboardAppearance: Brightness.light,
                    keyboardType: TextInputType.numberWithOptions(),
                    maxLines: 1,
                    onChanged: (value) {
                      leven = value;
                    },
                    style: TextStyle(
                        fontSize:
                            ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
                    cursorColor: mainColor,
                    decoration: new InputDecoration(
                        hintText: "请输入你的等级",
                        border: InputBorder.none,
                        hintStyle: TextStyle(
                            color: grey,
                            fontSize: ScreenUtil.getInstance()
                                .setSp(NORMAL_FONT_SIZE))),
                  )),
                ]),
              ),
              Container(
                height: 1,
                color: f0Color,
              ),
              new Container(
                width: ScreenUtil.getInstance().setWidth(550.00),
                height: ScreenUtil.getInstance().setWidth(DP_90),
                decoration: new BoxDecoration(
                    color: mainColor,
                    borderRadius: BorderRadius.circular(DP_80)),
                padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
                margin: EdgeInsets.only(top: 30),
                child: new FlatButton(
                  child: new Padding(
                    padding: EdgeInsets.all(0.0),
                    child: new Text(
                      "提交申请",
                      style: new TextStyle(
                          color: Colors.white,
                          fontSize:
                              ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
                    ),
                  ),
                  onPressed: () {
                    if (company == "" ||
                        wx_account == "" ||
                        wx_name == "" ||
                        name == "") {
                      Fluttertoast.showToast(msg: "请填写全部信息");
                      return;
                    }
                    showDialog(
                        context: context,
                        barrierDismissible: false,
                        builder: (_) {
                          return new NetLoadingDialog(
                            loadingText: "申请中...",
                            bgColor: Color.fromARGB(255, 0, 0, 0),
                            requestCallBack: null,
                            outsideDismiss: false,
                          );
                        });
                    register();
                  },
                ),
              )
            ],
          )),
    );
  }

  register() async {
    Map map = new Map();
    map['token'] = token;
    map['name'] = name;
    map['company'] = company;
    map['wx_account'] = wx_account;
    map['wx_name'] = wx_name;
    map['leven'] = leven;
    map['sign_type'] = SignType.APPLY_SHOUQUAN;
    Response<String> response =
        await HttpUtil().post(Api.APPLY_SHOUQUAN, data: getSign(map));
    Map userMap = json.decode(response.data);
    int code = userMap["code"];
    String msg = userMap["msg"];
    Navigator.of(context).pop(1);
    Fluttertoast.showToast(msg: msg);
    if (code == 1 && mounted) {
      Navigator.of(context).pop();
    }
  }
}
